package com.junzhiit.nengji.dao.impl;


import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.bainaeco.framework.base.dao.AbstractBaseDAO;
import com.bainaeco.framework.common.jdbc.StatementParameter;
import com.bainaeco.framework.enums.IsDelete;
import com.junzhiit.nengji.bo.ChainBO;
import com.junzhiit.nengji.dao.ChainDAO;
import com.junzhiit.nengji.model.Chain;
import com.junzhiit.nengji.model.enums.AuditStatus;
import com.junzhiit.nengji.model.enums.Is;

@Repository
public class ChainDAOImpl extends AbstractBaseDAO<Chain> implements ChainDAO{
	
	@Override
	public int getCount(Chain chain){
		StringBuffer sql=new StringBuffer();
		sql.append(" select count(*) from `chain`");
		sql.append(" where is_delete=?");
		
		
		StatementParameter param=new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		if(StringUtils.isNotBlank(chain.getSystemUserId())){
			sql.append(" and system_user_id=?");
			param.setString(chain.getSystemUserId());
		}
		
		
		return super.queryForInt(sql.toString(),param);
	}
	
	
	@Override
	public Chain getChain(Chain chain){
		StringBuffer sql=new StringBuffer();
		sql.append("select * from chain");
		sql.append(" where is_delete=?");
		
		StatementParameter param=new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		
		if(StringUtils.isNotBlank(chain.getSystemUserId())){
			sql.append(" and system_user_id=?");
			param.setString(chain.getSystemUserId());
		}
		
		return super.query(sql.toString(), Chain.class, param);
	}


	@Override
	public List<ChainBO> findAll(ChainBO chainBO,int pageNumber, int maxPageRow) {
		StringBuffer sql = new StringBuffer();
		sql.append("select i.`name` as industryName,sm.`name` as setMealName,c.* from `chain` c");
		sql.append(" inner join industry i on i.id = c.industry_id");
		sql.append(" left join set_meal sm on sm.id = c.set_meal_id");
		sql.append(" where c.is_delete = ?");
		StatementParameter params = new StatementParameter();
		params.setInt(IsDelete.NO.toIntValue());
		if(StringUtils.isNoneBlank(chainBO.getName())){
			sql.append(" and c.name like ?");
			params.setString("%"+chainBO.getName()+"%");
		}
		if(chainBO.getAuditStatus() != null){
			sql.append(" and c.audit_status = ?");
			params.setInt(chainBO.getAuditStatus());
		}
		return super.queryForPagingList(sql.toString(),ChainBO.class,params, pageNumber, maxPageRow);
	}


	@Override
	public int getTotalCount(ChainBO chainBO) {
		StringBuffer sql = new StringBuffer();
		sql.append("select count(c.id) from `chain` c");
		sql.append(" inner join industry i on i.id = c.industry_id");
		sql.append(" left join set_meal sm on sm.id = c.set_meal_id");
		sql.append(" where c.is_delete = ?");
		StatementParameter params = new StatementParameter();
		params.setInt(IsDelete.NO.toIntValue());
		if(StringUtils.isNoneBlank(chainBO.getName())){
			sql.append(" and c.name like ?");
			params.setString("%"+chainBO.getName()+"%");
		}
		if(chainBO.getAuditStatus() != null){
			sql.append(" and c.audit_status = ?");
			params.setInt(chainBO.getAuditStatus());
		}
		return super.queryForInt(sql.toString(),params);
	}
	
	
	public int queryTotal(String chainId) {
		StringBuffer sql=new StringBuffer();
		sql.append("select count(s.id) from shop s inner join chain c on s.chain_id=c.id");
		sql.append(" where c.id = ? and s.is_delete = ? and c.is_delete = ?");
		StatementParameter param=new StatementParameter();
		param.setString(chainId);
		param.setInt(Is.NO.toIntValue());
		param.setInt(Is.NO.toIntValue());
		return super.queryForInt(sql.toString(),param);
	}


	@Override
	public List<Chain> getChainList(Chain chain) {
		StringBuffer sql=new StringBuffer();
		sql.append("select * from chain");
		sql.append(" where is_delete=? and audit_status =? and is_expire =?");
		
		StatementParameter param=new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		param.setInt(AuditStatus.AUDITSUCCESS.toIntStatus());
		param.setInt(Is.NO.toIntValue());
		
		return super.queryForList(sql.toString(), Chain.class,param);
	}
}
